Getting Started with R & RStudio

CEU, AIIMS Bhopal

What are R and RStudio?

  • We use two programs that work together.

  • R is the Engine.

    • The programming language.
    • It does all the calculations.
    • It’s the “kitchen” (oven, knives, etc.).
  • RStudio is the Dashboard.

    • An “Integrated Development Environment” (IDE).
    • It’s the “organized countertop & recipe book” you use to operate the kitchen.

Our Setup cluster_rstudio RStudio (The Dashboard) cluster_r R (The Kitchen) Editor Editor (Recipe Book) Console Console Engine R Engine Console->Engine sends commands Env Environment (Ingredients) Files Files (Pantry) Engine->Env preps ingredients

The RStudio Interface

RStudio Interface: The Panes

  • What it is: Your text editor.
  • Use: Write and save your R Scripts (.R) and Quarto docs (.qmd). This is your “recipe book.”
  • Key Action: Ctrl + Enter (or Cmd + Enter) to run the current line.
  • What it is: Where R actually runs. You’ll see the > prompt.
  • Use: Test code, see outputs, and view error messages. This is like a “calculator” or “testing a single step.”
  • Note: Code typed here is not saved.
  • What it is: Your R session’s “memory.”
  • Use: See all objects (data, variables) you’ve created. These are your “prepped ingredients.”
  • History tab shows all your past commands.
  • Files: Browse your computer’s folders and files.
  • Plots: Where your graphs appear.
  • Packages: Manage your installed “appliances.”
  • Help: Look up help files for functions.

Saving Your Work: R Scripts

Your Console history is not saved. To save your work, you must use an R Script (.R file).

  • Go to File > New File > R Script.
  • Write your code in the Source Editor. This is your reproducible recipe.
  • Use comments (#) to explain your code.
  • Run code line-by-line using Ctrl + Enter.

my_recipe.R

# This is a comment
x <- 10 + 5
print(x)

➡️

Console

> x <- 10 + 5
> print(x)
[1] 15
> _

The “Where Am I?” Problem

This is the #1 source of confusion for new R users.

  • The Problem: You try to load a file read_csv("data.csv") and R says: “File not found!”
  • The Reason: R is always “standing” in one folder, its Working Directory.
  • Analogy: R is a cook at a single workbench. When you ask for "data.csv", R only looks on that workbench. If the file is in the pantry (your Desktop), R won’t find it.

The “Bad Solution” (Don’t Do This!)

You will see this code online. Do not use it.

# This is an ABSOLUTE path
setwd("C:/Users/YourName/Desktop/MyFolder")
  • Why it’s bad: This “hard-codes” your exact folder path.
  • Analogy: It’s like writing your specific home address in a recipe.
  • The Result: When you send your script to a colleague (who has a different “address”), your script breaks. It is not reproducible.

The “Good Solution”: RStudio Projects

We will NEVER use setwd(). We will ALWAYS use RStudio Projects.

  • What it is: A single file (.Rproj) that lives in your project’s main folder.
  • The Magic: When you double-click the .Rproj file to open RStudio, it automatically sets your Working Directory to that folder.
  • Analogy: It’s a self-contained “kitchen-in-a-box.” All your recipes, ingredients, and final dishes live inside one organized, shareable folder.

The RStudio Project Workflow

This is the Golden Rule for all your work.

The Project Workflow cluster_project My_Recipe_Book/ (Your Project Binder) cluster_data data/ cluster_scripts scripts/ cluster_output output/ RPROJ My_Recipe_Book.Rproj (Double-click this!) SCRIPT my_recipe.R RPROJ->SCRIPT sets 'home base' DATA ingredients.csv SCRIPT->DATA reads from 'data/ingredients.csv' PLOT my_cake.png SCRIPT->PLOT writes to 'output/my_cake.png'

  • Your script my_recipe.R can use relative paths like read_csv("data/ingredients.csv").
  • This will work for anyone who downloads your project folder.

Expanding Your Power: Packages

  • The Concept: “Base R” is your basic kitchen. Packages are “fancy appliances” you get for specific jobs.
  • Analogy:
    • Want to wrangle data? Get the dplyr “food processor.”
    • Want to make graphs? Get the ggplot2 “stand mixer with piping kit.”
    • Want a bunch of tools? Get the tidyverse “full professional kitchen suite.”

Packages: The 2-Step Process

This is a critical concept.

Step 1: INSTALL

  • What: “Buy the appliance” from the store (CRAN) and have it delivered to your kitchen.
  • When: Only ONCE per computer.
  • How:
install.packages("tidyverse")

(Run this in your Console)

Step 2: LOAD

  • What: “Plug the appliance in” to the wall to use it for this cooking session.
  • When: EVERY TIME you restart R.
  • How:
library(tidyverse)

(Put this at the top of your R Script)

Session Recap: Best Practices

  • Always organize your work using RStudio Projects (your “kitchen-in-a-box”).
  • Write your code in R Scripts (your “recipes”).
  • Run code from your script using Ctrl + Enter (or Cmd + Enter).
  • Use comments (#) to explain why you are doing something.
  • Load all your required packages (using library()) at the very top of your script.

How to Get Help (Your Most Important Skill)

How to Get Help start Need help? q1 Know the function name? (e.g., 'mean') start->q1 q2 Know what you want, but not the function? q1->q2 No ans1 Use `?mean` or Look at 'Help' tab q1->ans1 Yes q3 Got an error message? q2->q3 No ans2 Use `??search term` (e.g., `??'t-test'`) q2->ans2 Yes q4 Want to generate, debug, or explain code? q3->q4 No ans3 Copy the *exact* error. Paste into Google! q3->ans3 Yes q4->ans1 No... ans4 Use Generative AI (e.g., Copilot) *Be specific!* *No private data!* q4->ans4 Yes

Questions?